# -*- coding:utf-8 -*-

from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 30
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class ClassfyAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()

    def addClassfy(self, classfyInfo):
        classfyName = classfyInfo['classfyName']
        classfyPid = classfyInfo['classfyPid']
        classfyCode = classfyInfo['classfyCode']
        classfyStatus = classfyInfo['classfyStatus']
        classfySort = classfyInfo['classfySort']

        cur = self.conn.cursor()
        sql = "insert into zzh_classfy (classfy_name,classfy_pid,classfy_code,classfy_status,classfy_sort) VALUES (%s,%s,%s,%s,%s)"
        result = cur.execute(sql, [classfyName, classfyPid, classfyCode, classfyStatus, classfySort])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    def updateClassfy(self, classfyInfo, classfyId):
        classfyName = classfyInfo['classfyName']
        classfyPid = classfyInfo['classfyPid']
        classfyCode = classfyInfo['classfyCode']
        classfyStatus = classfyInfo['classfyStatus']
        classfySort = classfyInfo['classfySort']
        cur = self.conn.cursor()
        sql = "update zzh_classfy set classfy_name=%s,classfy_pid=%s,classfy_code=%s,classfy_status=%s,classfy_sort=%s WHERE classfy_id=%s "
        result = cur.execute(sql, [classfyName, classfyPid, classfyCode, classfyStatus, classfySort, classfyId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    def queryClassfy(self, page_index):  # zzh_declar表查询记录

        cur = self.conn.cursor()
        sql = "select * from zzh_classfy"
        total_count = cur.execute(sql)
        classfyList = cur.fetchall()
        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            classfyListPart = classfyList[start: end]

            classfyListPartJson = Table2Json.classfy_list_to_json(classfyListPart)
            return total_count, classfyListPartJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            classfyListPart = classfyList[start:]
            classfyListPartJson = Table2Json.classfy_list_to_json(classfyListPart)
            return total_count, classfyListPartJson

    def deleteClassfyById(self, classfyId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_classfy WHERE classfy_id=%s"
        deleteClassfyResultOfDatabase = cur.execute(sql, [classfyId])
        self.conn.commit()
        if deleteClassfyResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def queryClassfyName(self):
        cur = self.conn.cursor()
        sql = "SELECT classfy_name,classfy_pid from zzh_classfy"
        result = cur.execute(sql)

        queryClassfyAndNameResult = cur.fetchall()
        queryClassfyAndNameResultJSON = Table2Json.classfy_name_list_to_json(queryClassfyAndNameResult)  # 再封装成JSON格式

        cur.close()
        self.conn.close()
        return queryClassfyAndNameResultJSON

    def query_classfy_one(self, classfy_code):

        sql = "select * from zzh_classfy WHERE classfy_pid=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [classfy_code])

        if rs:
            classList = cur.fetchall()
            classJson = Table2Json.classfy_list_to_json(classList)
            cur.close()
            self.conn.close()
            return classJson
        else:
            return None

